﻿package {
	import flash.display.MovieClip;
	import flash.text.*;
	import flash.events.*;

	class PageProperty extends MovieClip {
		
		private var page:EditWindow;
		
		public var title:TextField;
		private var colorText:TextField;
		private var color:uint;
		private var strColor:String;
		
		public function PageProperty(page:EditWindow) {
			this.page = page;
			setupTitle();
			setupColor();
			color = 0xFFFFFF;
			changeColor();
			colorText.addEventListener(KeyboardEvent.KEY_DOWN, colorListener);
		}
		
		public function setColor(col:uint) { color = col; changeColor(); colorText.text = "#" + color.toString(16); }
		public function getTitle():String { return title.text; }
		public function getColor():String { return "0x" + color.toString(16); }
		
		private function setupTitle() {
			title = new TextField();
			title.text = "New Page";
			title.x = 120; title.y = 40;
			title.background = true; title.backgroundColor = 0xDDDDDD;
			title.border = true; title.borderColor = 0x000000;			
			title.width = 160; title.height = 20;
			title.type = TextFieldType.INPUT;
			addChild(title);
		}
		
		private function setupColor() {
			colorText = new TextField();
			colorText.text = "White";
			strColor = "White";
			colorText.background = true; colorText.backgroundColor = 0xDDDDDD;
			colorText.border = true; colorText.borderColor = 0x000000;		
			colorText.x = 120; colorText.y = 70;
			colorText.width = 80; colorText.height = 20;
			colorText.type = TextFieldType.INPUT;
			addChild(colorText);
		}
		
		private function colorListener(event:KeyboardEvent){
			var converter:ColorConverter = new ColorConverter();
		    if(event.charCode == 13){ //If Enter
				var tempColor = converter.convert(colorText.text);
			    if (tempColor != null) {
					color = tempColor;
					strColor = colorText.text;
				}
				changeColor();
			}
		}
		
		private function changeColor() {
			page.graphics.clear();
			page.graphics.beginFill(color);
			page.graphics.drawRect(0,0,924,668);
			page.graphics.endFill();
		}
	}
}